package com.yc.jpyy.mobile.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yc.jpyy.base.bean.RestResponse;
import com.yc.jpyy.base.config.Config;
import com.yc.jpyy.base.controller.BaseController;
import com.yc.jpyy.mobile.dao.BizDao;
import com.yc.jpyy.mobile.service.BizService;

@Controller
@RequestMapping(value = "/student")
public class AddEvaluateController extends BaseController {
	
	@Autowired
	private BizService bizService;
	

	@Autowired
	private BizDao bizDao;
	private static Logger logger = LoggerFactory.getLogger(AddEvaluateController.class);
	
	/**
	 * 添加教练或驾校评价
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/studentEvaluate", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String addStudentEvaluate(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			//学员Id
			String studentId = request.getParameter("studentId");
			//教练或驾校ID
			String objectId = request.getParameter("objectId");
			//1,教练或2,驾校
			String type = request.getParameter("type");
			//星评的数量
			String overall = request.getParameter("overall");
			//科目
			String subjectId = request.getParameter("subjectId");
			//评价内容
			String content = request.getParameter("content");
			
			if((null !=studentId && !studentId.isEmpty())
					&& (null !=objectId && !objectId.isEmpty())
					&& (null !=type && !type.isEmpty())
					&& (null !=overall && !overall.isEmpty())
					&& (null !=content && !content.isEmpty())){
				Map<String, Object> valMap = new HashMap<String, Object>();
				valMap.put("studentId", studentId);
				valMap.put("objectId", objectId);
				valMap.put("type", type);
				valMap.put("overall", overall);
				valMap.put("subjectId", subjectId);
				valMap.put("content", content);
				response =  bizService.addStudentEvaluate(valMap,response);
			}else{
				response.setErrorcode(1);
				response.setMessage("缺少必填参数!");
			}
		} catch (Exception e) {
			logger.info("addStudentEvaluate："+ e.toString());
			response.setErrorcode(1);
			response.setMessage("评价失败，请稍候重试！");
		} finally {
			return response.toJSON();
		}
	}
	
	/**
	 * 添加视频评价
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/videoEvaluate", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String addVideoEvaluate(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			//用户Id
			String userId = request.getParameter("userId");
			//视频ID
			String videoId = request.getParameter("videoId");
			//星评的数量
			String overall = request.getParameter("overall");
			//评价内容
			String content = request.getParameter("content");
			
			if((null !=userId && !userId.isEmpty())
					&& (null !=videoId && !videoId.isEmpty())
					&& (null !=overall && !overall.isEmpty())
					&& (null !=content && !content.isEmpty())){
				Map<String, Object> valMap = new HashMap<String, Object>();
				valMap.put("userId", userId);
				valMap.put("videoId", videoId);
				valMap.put("overall", overall);
				valMap.put("content", content);
				response =  bizService.addVideoEvaluate(valMap,response);
			}else{
				response.setErrorcode(1);
				response.setMessage("缺少必填参数!");
			}
		} catch (Exception e) {
			logger.info("addVideoEvaluate："+ e.toString());
			response.setErrorcode(1);
			response.setMessage("评价失败，请稍候重试！");
		} finally {
			return response.toJSON();
		}
	}
	
	/**
	 * 获取驾校或教练评价
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getStudentEvaluate", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String getStudentEvaluate(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			//类型（1教练，2驾校）
			String type = request.getParameter("type");
			//教练ID
			String teacherId = request.getParameter("teacherId");
			//驾校ID
			String driverId = request.getParameter("driverId");
			if(null !=type && !type.isEmpty()){
				Map<String, Object> valMap = new HashMap<String, Object>();
				valMap.put("teacherId", teacherId);
				valMap.put("driverId", driverId);
				valMap.put("type", type);
				valMap.put("imgUrl", Config.getImageAddress());
				response =  bizService.getStudentEvaluate(valMap,response);
			}else{
				response.setErrorcode(1);
				response.setMessage("缺少必填参数!");
			}
		} catch (Exception e) {
			logger.info("getStudentEvaluate："+ e.toString());
			response.setErrorcode(1);
			response.setMessage("获取评价失败，请稍候重试！");
		} finally {
			return response.toJSON();
		}
	}
	
	/**
	 * 获取视频评价
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getVideoEvaluate", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String getVideoEvaluate(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			//视频ID
			String videoId = request.getParameter("videoId");
			if(null !=videoId && !videoId.isEmpty()){
				String imgUrl = Config.getImageAddress();
				response =  bizService.getVideoEvaluate(videoId,imgUrl,response);
			}else{
				response.setErrorcode(1);
				response.setMessage("缺少必填参数!");
			}
		} catch (Exception e) {
			logger.info("getVideoEvaluate："+ e.toString());
			response.setErrorcode(1);
			response.setMessage("获取评价失败，请稍候重试！");
		} finally {
			return response.toJSON();
		}
	}
}
